#!/bin/bash

source ./nginx.env
echo '正在初始化环境'
home=$ALBERTMU_NGINX_WORKSPACE
if [[ -d $home ]]; then
    echo "初始化环境失败，文件夹【${home}】已存在。"
    exit 1
fi
mkdir $home && mkdir $home/conf && mkdir $home/html && mkdir $home/logs
cd $home/conf && touch hello.conf
cat <<EOF > $home/conf/hello.conf
server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html/hello;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
EOF
cd $home/html && mkdir hello && cd hello && touch index.html

cat <<EOF > $home/html/hello/index.html
<html>
    <body>
        <h1>Hello world!</h1>
    </body>
</html>
EOF

echo '初始化成功'
echo '正在部署容器'

docker run -d --name $ALBERTMU_C_NAME \
 -p 6680:80 \
 --privileged=true \
 --restart=always \
 --net $VNET_NAME \
 -v ${home}/html:/usr/share/nginx/html:ro \
 -v ${home}/conf:/etc/nginx/conf.d:ro \
 -v ${home}/logs:/var/log/nginx \
 nginx:$ALBERTMU_NGINX_VERSION
echo 'nginx已部署完毕！'
